package com.anrongtec.zcpt.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.io.UnsupportedEncodingException;
import java.util.UUID;

/**
 * 
 * @author dongtianhao
 *
 */
@SuppressLint("NewApi")
public class DeviceFactory {
	protected static final String PREFS_DEVICE_UUID = "prefs_device_uuid";
	protected static final String PREFS_DEVICE_ID = "prefs_device_id";
	/** 手机制式类型 */
	protected static final String PREFS_PHONE_TYPE = "prefs_phone_type";
	protected static final String PREFS_MAC_ADDRESS = "prefs_mac_address";
	protected static final String PREFS_ANDROID_ID = "prefs_android_id";
	protected static final String PREFS_IMSI_CODE = "prefs_imsi_code";
	protected static UUID uuid;
	private static Context context;
	private static volatile DeviceFactory factory;
	private static SharedPreferences prefs;
	private TelephonyManager mTelephonyMgr;

	private DeviceFactory(Context ctx) {
		if (context == null) {
			context = ctx.getApplicationContext();
		}
	}

	public static DeviceFactory getInstance(Context context) {
		if (factory == null) {
			synchronized (DeviceFactory.class) {
				if (factory == null) {
					factory = new DeviceFactory(context);
				}
			}
		}
		return factory;
	}

	/**
	 * 
	 * @Title: getDeviceUUID @Description:加密算法过后的设备ID，与设备无关，只做唯一约束性
	 *         设备管理应该直接用设备ID @param @return 设定文件 @return String 返回类型 @throws
	 */
	public String getDeviceUUID() {
		if (prefs == null) {
			prefs = context.getApplicationContext().getSharedPreferences(PreferenceName.DEVICE_INFO, 0);
		}
		final String id = prefs.getString(PREFS_DEVICE_UUID, null);
		if (!TextUtils.isEmpty(id)) {
			return id;
			// uuid = UUID.fromString(id);
		} else {
			final String androidId = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
			try {
				if (!"9774d56d682e549c".equals(androidId)) {
					uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf8"));
				} else {
					if (mTelephonyMgr == null) {
						mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
					}
					final String deviceId = mTelephonyMgr.getDeviceId();
					uuid = deviceId != null ? UUID.nameUUIDFromBytes(deviceId.getBytes("utf8")) : UUID.randomUUID();
				}
			} catch (UnsupportedEncodingException e) {
				throw new RuntimeException(e);
			}

			// Write the value out to the prefs file
			String uid = uuid.toString().replace("-", "");
			prefs.edit().putString(PREFS_DEVICE_UUID, uid).commit();
			return uid;
		}
	}

	/**
	 * 
	 * @Title: getIMEI @Description:
	 *         获取设备的IMEI码，如果是GSM网络，返回IMEI；如果是CDMA网络，返回MEID @return String
	 *         返回类型 @throws
	 */
	public String getIMEI() {
		if (prefs == null) {
			prefs = context.getApplicationContext().getSharedPreferences(PreferenceName.DEVICE_INFO, 0);
		}
		String id = prefs.getString(PREFS_DEVICE_ID, null);
		if (!TextUtils.isEmpty(id)) {
			return id;
		} else {
			if (mTelephonyMgr == null) {
				mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
			}
			id = mTelephonyMgr.getDeviceId();
			prefs.edit().putString(PREFS_DEVICE_ID, id).commit();
			return id;
		}
	}

	/**
	 * 
	 * @Title: getIMSI @Description:获取ISMI码，SIM卡标识 @param @return @return String
	 *         返回类型 @throws
	 */
	public String getIMSI() {
		if (prefs == null) {
			prefs = context.getApplicationContext().getSharedPreferences(PreferenceName.DEVICE_INFO, 0);
		}
		String imsi = prefs.getString(PREFS_IMSI_CODE, null);
		if (!TextUtils.isEmpty(imsi)) {
			return imsi;
		} else {
			if (mTelephonyMgr == null) {
				mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
			}
			imsi = mTelephonyMgr.getSubscriberId();
			prefs.edit().putString(PREFS_IMSI_CODE, imsi).commit();
			return imsi;
		}
	}

	/**
	 * 获取手机类型,厂商类型
	 * 
	 * @Title: getPhoneModel @Description: TODO(这里用一句话描述这个方法的作用) @return String
	 *         返回类型 @throws
	 */
	public String getPhoneModel() {

		return android.os.Build.MODEL;

	}

	/**
	 * 获取手机制式类型
	 * 
	 * @Title: getPhoneType @Description: TODO(这里用一句话描述这个方法的作用) @return int
	 *         返回类型 @returnValue int PHONE_TYPE_CDMA 手机制式为CDMA，电信 @returnValue
	 *         int PHONE_TYPE_GSM 手机制式为GSM，移动和联通 @returnValue int
	 *         PHONE_TYPE_NONE 手机制式未知 @throws
	 */
	public int getPhoneType() {
		if (prefs == null) {
			prefs = context.getApplicationContext().getSharedPreferences(PreferenceName.DEVICE_INFO, 0);
		}
		int type = prefs.getInt(PREFS_PHONE_TYPE, -1);
		if (type != -1) {
			return type;
		} else {
			if (mTelephonyMgr == null) {
				mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
			}
			type = mTelephonyMgr.getPhoneType();
			prefs.edit().putInt(PREFS_PHONE_TYPE, type).commit();
			return type;
		}
	}

	/**
	 * 获取mac地址
	 * 
	 * @Title: getMacAddress @Description: TODO(这里用一句话描述这个方法的作用) @param @return
	 *         设定文件 @return String 返回类型 @throws
	 */
	public String getMacAddress() {
		if (prefs == null) {
			prefs = context.getApplicationContext().getSharedPreferences(PreferenceName.DEVICE_INFO, 0);
		}
		String mac = prefs.getString(PREFS_MAC_ADDRESS, null);
		if (!TextUtils.isEmpty(mac)) {
			return mac;
		} else {
			WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
			WifiInfo wifiInfo = wifiManager.getConnectionInfo();
			mac = wifiInfo.getMacAddress();
			prefs.edit().putString(PREFS_MAC_ADDRESS, mac);
			return mac;
		}

	}

	/**
	 * 获取设备序列号
	 * 
	 * @return
	 */
	public String getDeviceSN() {
		if (prefs == null) {
			prefs = context.getApplicationContext().getSharedPreferences(PreferenceName.DEVICE_INFO, 0);
		}
		String androidId = prefs.getString(PREFS_ANDROID_ID, null);
		if (!TextUtils.isEmpty(androidId)) {
		} else {
			androidId = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
			prefs.edit().putString(PREFS_ANDROID_ID, androidId);
		}
		return androidId;
	}

}